package com.qfhd.soft.cjzcore.TaskCenter.GDT;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.widget.FrameLayout;

import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.Listener.CJAdListener;
import com.qfhd.soft.cjzcore.TaskCenter.CJAdCenter;
import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.ADSize;
import com.qq.e.ads.nativ.NativeExpressAD;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.ads.nativ.NativeExpressMediaListener;
import com.qq.e.comm.constants.AdPatternType;
import com.qq.e.comm.util.AdError;

import java.util.ArrayList;
import java.util.List;

public class CJGDT_Navi implements NativeExpressAD.NativeExpressADListener {

    private final int adSize = 10;

    private NativeExpressAD nativeExpressAD;
    private NativeExpressADView nativeExpressADView;
    private FrameLayout container;
    private CJAdListener statusListener;
    private boolean isPreloadVideo = false;
    private int width;
    private int height;
    private boolean isVideo = false;
    private List<NativeExpressADView> ad_list = new ArrayList<>();

    private static CJGDT_Navi gdt_navi;

    public static CJGDT_Navi getInstance() {
        if (gdt_navi == null) {
            synchronized (CJGDT_Navi.class) {
                gdt_navi = new CJGDT_Navi();
            }
        }
        return gdt_navi;
    }

    private void releseAd() {
        // 使用完了每一个NativeExpressADView之后都要释放掉资源
        if (nativeExpressADView != null) {
            nativeExpressADView.destroy();
        }

        if(ad_list != null && ad_list.size() > 0){
            ad_list.remove(0);
        }
    }

    //拥有固定 不用重复请求
    public void show(Context context, int width, int height, FrameLayout mExpressContainer, Activity activity, CJAdListener statusListener) {
        CJLogger.v("GDT Navi width:" + width + "  height:" + height);

        releseAd();

        this.width = width;
        this.height = height;

        container = mExpressContainer;
        this.statusListener = statusListener;


        CJLogger.v("GDT Navi ad_list size:" + ad_list.size());
        if(ad_list.size() > 0){
            beginAd(ad_list.get(0));
        }
        else{
            nativeExpressAD = new NativeExpressAD((Context) activity, new ADSize(width, height), CJGDTsdk.APPID, CJGDTsdk.NaviAd, this); // 这里的Context必须为Activity
            nativeExpressAD.setVideoPlayPolicy(getVideoPlayPolicy(VideoOption.AutoPlayPolicy.ALWAYS, context));  // 本次拉回的视频广告，在用户看来是否为自动播放的
            nativeExpressAD.loadAD(adSize);
        }
    }

    public static int getVideoPlayPolicy(int autoPlayPolicy, Context context){
        if(autoPlayPolicy == VideoOption.AutoPlayPolicy.ALWAYS){
            return VideoOption.VideoPlayPolicy.AUTO;
        }else if(autoPlayPolicy == VideoOption.AutoPlayPolicy.WIFI){
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifiNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            return wifiNetworkInfo != null && wifiNetworkInfo.isConnected() ? VideoOption.VideoPlayPolicy.AUTO
                    : VideoOption.VideoPlayPolicy.MANUAL;
        }else if(autoPlayPolicy == VideoOption.AutoPlayPolicy.NEVER){
            return VideoOption.VideoPlayPolicy.MANUAL;
        }
        return VideoOption.VideoPlayPolicy.UNKNOWN;
    }

    private ADSize getAdSize() {
        if (nativeExpressADView == null) {
            return new ADSize(0,0);
        }
        if (height == 0) {
            height = -2;//自动适应高度
        }
        width = -1;//自动适应宽度
        return new ADSize(width,height);
    }

    @Override
    public void onADLoaded(List<NativeExpressADView> list) {
        ad_list.addAll(list);
        beginAd(ad_list.get(0));
    }

    private void beginAd(NativeExpressADView ad){
        if (nativeExpressADView != null) {
            nativeExpressADView.destroy();
        }

        if (container.getVisibility() != View.VISIBLE) {
            container.setVisibility(View.VISIBLE);
        }

        if (container.getChildCount() > 0) {
            container.removeAllViews();
        }

        nativeExpressADView = ad;
        nativeExpressADView.setAdSize(getAdSize());
//        Log.i(TAG, "onADLoaded, video info: " + getAdInfo(nativeExpressADView));
        if (nativeExpressADView.getBoundData().getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
            nativeExpressADView.setMediaListener(mediaListener);
            isVideo = true;
            if (isPreloadVideo) {
                // 预加载视频素材，加载成功会回调mediaListener的onVideoCached方法，失败的话回调onVideoError方法errorCode为702。
                nativeExpressADView.preloadVideo();
            }
        } else {
            isPreloadVideo = false;
        }
        if (!isPreloadVideo) {
            // 广告可见才会产生曝光，否则将无法产生收益。
            container.addView(nativeExpressADView);
            nativeExpressADView.render();
        }
    }

    private NativeExpressMediaListener mediaListener = new NativeExpressMediaListener() {
        @Override
        public void onVideoInit(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoLoading(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoCached(NativeExpressADView nativeExpressADView) {
            // 视频素材加载完成，此时展示视频广告不会有进度条。
            if (isPreloadVideo && nativeExpressADView != null) {
                if (container.getChildCount() > 0) {
                    container.removeAllViews();
                }
                // 广告可见才会产生曝光，否则将无法产生收益。
                container.addView(nativeExpressADView);
                nativeExpressADView.render();
            }
        }

        @Override
        public void onVideoReady(NativeExpressADView nativeExpressADView, long l) {

        }

        @Override
        public void onVideoStart(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoPause(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoComplete(NativeExpressADView nativeExpressADView) {

        }

        @Override
        public void onVideoError(NativeExpressADView nativeExpressADView, AdError adError) {

        }

        @Override
        public void onVideoPageOpen(NativeExpressADView nativeExpressADView) {
            if (statusListener != null) {
                statusListener.onStatus(CJAdCenter.AD_Status_HideDialog);
            }
        }

        @Override
        public void onVideoPageClose(NativeExpressADView nativeExpressADView) {
            if (statusListener != null) {
                statusListener.onStatus(CJAdCenter.AD_Status_ShowDialog);
            }
        }
    };

    @Override
    public void onRenderFail(NativeExpressADView nativeExpressADView) {

    }

    @Override
    public void onRenderSuccess(NativeExpressADView nativeExpressADView) {

    }

    @Override
    public void onADExposure(NativeExpressADView nativeExpressADView) {

    }

    @Override
    public void onADClicked(NativeExpressADView nativeExpressADView) {
        if (statusListener != null) {
            statusListener.onStatus(CJAdCenter.AD_Status_Click);
        }
    }

    @Override
    public void onADClosed(NativeExpressADView nativeExpressADView) {
        if (statusListener != null) {
            statusListener.onStatus(CJAdCenter.AD_Status_Close);
        }
    }

    @Override
    public void onADLeftApplication(NativeExpressADView nativeExpressADView) {

    }

    @Override
    public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {
        if (statusListener != null) {
            statusListener.onStatus(CJAdCenter.AD_Status_OpenFrame);
        }
    }

    @Override
    public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {
        if (statusListener != null) {
            statusListener.onStatus(CJAdCenter.AD_Status_Close);
        }
    }

    @Override
    public void onNoAD(AdError adError) {
        CJLogger.v("GDT Navi error code:" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
    }

}
